package com.szsh.aiot.framework.config;

import org.influxdb.BatchOptions;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.impl.InfluxDBMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * influxdb配置
 *
 * @author luohuawuyin
 *
 */
@Configuration
public class InfluxDbConfig {

    @Value("${spring.influx.url}")
    private String url;

    @Value("${spring.influx.user}")
    private String userName;

    @Value("${spring.influx.password}")
    private String password;

    @Value("${spring.influx.database}")
    private String database;

    @Value("${spring.influx.retentionPolicy}")
    private String retentionPolicy;

    @Bean
    public InfluxDB initInfluxDb() {
        InfluxDB influxDB = InfluxDBFactory.connect(url, userName, password);
        influxDB.setDatabase(database);
        influxDB.setRetentionPolicy(retentionPolicy);
        influxDB.setLogLevel(InfluxDB.LogLevel.BASIC);
        // 开启influxdb批量保存
        influxDB.enableBatch(BatchOptions.DEFAULTS);
        return influxDB;
    }

    @Bean
    public InfluxDBMapper initMapper(InfluxDB influxDB) {
        return new InfluxDBMapper(influxDB);
    }

}
